package com.bestvike.stone.spring.shiro.annotation.handler;

import com.bestvike.stone.spring.shiro.SecurityUtils;
import com.bestvike.stone.spring.shiro.annotation.RequiresUser;
import com.bestvike.stone.spring.shiro.authz.exception.AuthorizationException;
import com.bestvike.stone.spring.shiro.authz.exception.authc.UnauthenticatedException;

import java.lang.annotation.Annotation;

/**
 * 登录用户注解处理器
 */
public final class UserAnnotationHandler implements AuthorizingAnnotationHandler {
    /**
     * 获取支持的注解类型
     */
    @Override
    public Class<? extends Annotation> getAnnotationClass() {
        return RequiresUser.class;
    }

    /**
     * 断言权限
     */
    @Override
    public void assertAuthorized(Annotation annotation) throws AuthorizationException {
        if (SecurityUtils.isAuthenticated())
            return;
        throw new UnauthenticatedException();
    }
}
